package com.rd.teacher.jichao.day28;

public class Test1 {

    // 需要记录前一个节点  pre
    // 当前节点  current
    // 和原来的下一个节点  tmp
    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode current = head;

        while (current != null){
            // 2
            ListNode tmp = current.next;
            // 1->2 变为 1->null
            current.next = pre;

            // 两个指针都后移一位
            pre = current;
            current = tmp;
        }
        return pre;
    }

    public static void main(String[] args) {
        // 如果要构造一个从1-n的链表
        ListNode head = new ListNode(1);
        int n = 5;
        // 1->2-> ... -> n
        ListNode pre = head;
        for (int i = 2; i <= n; i++) {
            // i=2  head.next = listNode
            // i=3
            ListNode listNode = new ListNode(i);
            pre.next = listNode;
            pre = listNode;
        }

        ListNode result = reverseList(head);
        System.out.println(result);
    }
}
